*** Table 6 ***	

use "$data/simulated_df.dta", clear 

* Firms who have applied to both conventional and open
gen applied_conv_before18 = conv==1 & year<2019
bys firm_id: egen maxapplied = max(applied_conv_before18)

est clear 
foreach var in any_post_total_fpds_over50k ///
	any_post_total_patent any_post_total_hi_orig ///
	post_any_vc any_post_total_allsbir_diff {
		if "`var'"=="any_post_total_patent" {
			local stub "pat"
			local head "Patent"
			local yr "1095"
		}
		if "`var'"=="any_post_total_fpds_over50k" {
			local stub "fpds"
			local head "Non-SBIR Contract"
			local yr "1095"
		}
		if "`var'"=="post_any_vc" {
			local stub "vc"
			local head "VC"
			local yr "1095"
		}
		if "`var'"=="any_post_total_hi_orig" {
			local stub "orig"
			local head "High Originality"
			local yr "1095"
		}
		if "`var'"=="any_post_total_allsbir_diff" {
			local stub "allsbir"
			local yr ""
		}
		
		eststo `stub'1: areg `var'`yr' i.select $rddmain_controls employees_at_app age i.any_pre_total_allsbir ///
			[aweight=weight] if  maxapplied==1 & open==1 & phase==1, ///
			cluster(topic_n) absorb(topic_n)
		estadd scalar R= e(r2): `stub'1
		estadd local pd "2017-19": `stub'1
		estadd local prop "First": `stub'1
		estadd local controls "No": `stub'1
		estadd local techfe "No": `stub'1
		qui sum `var'`yr' if maxapplied==1 & open==1 & phase==1
		estadd scalar ymean = r(mean): `stub'1
		
		eststo `stub'2: areg `var'`yr' i.select $rddmain_controls employees_at_app age i.any_pre_total_allsbir ///
			employees_at_app age i.topic0 [aweight=weight] ///
			if  maxapplied==1 & open==1& phase==1, ///
			cluster(topic_n) absorb(topic_n)
		estadd scalar R= e(r2): `stub'2
		estadd local pd "2017-19": `stub'2
		estadd local prop "First": `stub'2
		estadd local controls "Yes": `stub'2
		estadd local techfe "No": `stub'2
		qui sum `var'`yr' if maxapplied==1 & open==1 & phase==1
		estadd scalar ymean = r(mean): `stub'2
		
		eststo `stub'3: areg `var'`yr' i.select $rddmain_controls employees_at_app age i.any_pre_total_allsbir ///
			i.full_tag_n25 [aweight=weight] ///
			if  maxapplied==1 & open==1& phase==1, ///
			cluster(topic_n) absorb(topic_n)
		estadd scalar R= e(r2): `stub'3
		estadd local pd "2017-19": `stub'3
		estadd local prop "First": `stub'3
		estadd local controls "Yes": `stub'3
		estadd local techfe "Yes": `stub'3
		qui sum `var'`yr' if maxapplied==1 & open==1 & phase==1
		estadd scalar ymean = r(mean): `stub'3
		
		
	}

esttab fpds1 vc1 pat1 orig1 allsbir1 using "$tables/table6a.tex", ///
	$general $labels ///
	keep(1.select) ///
	scalars("controls Controls" "techfe Narrow Tech FE" "prop Proposal" ///
		    "pd Time Period" "ymean Outcome Mean") ///
	postfoot("\midrule \end{tabular} \end{footnotesize} ") ///
	prehead("\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \begin{footnotesize} \begin{tabular}{l*{5}{>{\centering\arraybackslash}p{0.09\textwidth}}}  \toprule \multicolumn{6}{c}{Panel A: Baseline Specification} \\ \midrule & \multicolumn{1}{c}{$\mathbbm{1}$(DoD} & \multicolumn{1}{c}{\multirow{2}{*}{$\mathbbm{1}$(VC)}} & \multicolumn{1}{c}{\multirow{2}{*}{$\mathbbm{1}$(Patent)}} & \multicolumn{1}{c}{$\mathbbm{1}$(High} & \multicolumn{1}{c}{$\mathbbm{1}$($> 2$} \\ & \multicolumn{1}{c}{Contract)} & & & \multicolumn{1}{c}{Orig Pat)} & \multicolumn{1}{c}{SBIR)} \\ \cmidrule(lr){2-2}  \cmidrule(lr){3-3}  \cmidrule(lr){4-4} \cmidrule(lr){5-5} \cmidrule(lr){6-6} ")
	
esttab fpds3 vc3 pat3 orig3 allsbir3 using "$tables/table6b.tex", ///
	$general $labels ///
	keep(1.select) ///
	scalars("controls Controls" "techfe Narrow Tech FE" "prop Proposal" ///
		    "pd Time Period" "ymean Outcome Mean") ///
	postfoot("\bottomrule \end{tabular} \end{footnotesize} \\ ") ///
	prehead("\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi} \begin{footnotesize} \begin{tabular}{l*{5}{>{\centering\arraybackslash}p{0.09\textwidth}}} \multicolumn{6}{c}{Panel B: Controls with Technology Fixed Effects} \\ \midrule ")
